//
//  QDViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/31.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "QDViewController.h"

#import "HYCalendarView.h"
@interface QDViewController (){
    NSMutableArray *array;
    HYCalendarView *calendarView;
}
@property (weak, nonatomic) IBOutlet UIImageView *images;
@property (weak, nonatomic) IBOutlet UILabel *labName;
@property (weak, nonatomic) IBOutlet UILabel *labJF;
@property (weak, nonatomic) IBOutlet UIButton *btnQD;
@property (weak, nonatomic) IBOutlet UIView *views;

@property (nonatomic,strong)NSDictionary *dic;

@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *imgUrl;

@end

@implementation QDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self showNav:@"签到"];
    array = [NSMutableArray array];
    [self loadData];
    
}

- (void)loadUI{
    _images.layer.cornerRadius = 39.0;
    _images.layer.masksToBounds = YES;
    _views.layer.cornerRadius = 10.0;
    
    _labJF.text = [NSString stringWithFormat:@"%@分",[_dic objectForKey:@"userPoints"]];
    
    [_images sd_setImageWithURL:[NSURL URLWithString:_imgUrl] placeholderImage:[UIImage imageNamed:@"login"]];
    _labName.text = _name;
    
    calendarView = [[HYCalendarView alloc] init];
    calendarView.frame = CGRectMake(0, 0, _views.frame.size.width, 400);
    calendarView.layer.cornerRadius = 10.0;
    calendarView.layer.masksToBounds = YES;
    
    [_views addSubview:calendarView];
    
    NSMutableArray *arr = [NSMutableArray array];
    for (NSDictionary *op in array) {
        NSString *isSign = [NSString stringWithFormat:@"%li",[[op objectForKey:@"isSign"] integerValue]];
        NSString *date = [self getNewDate:[op objectForKey:@"signDate"]];
        NSString *str = [date substringFromIndex:8];
        NSInteger tags = [str integerValue] - 4;
        if ([isSign integerValue] == 2) {
            [arr addObject:[NSNumber numberWithInteger:tags]];
        }
    }
    //设置已经签到的天数日期
    calendarView.signArray = arr;
    
    //当前日期
    calendarView.date = [NSDate date];
    
    //判断今天是否签到
    NSDictionary *op = [array lastObject];
    NSString *isSign = [NSString stringWithFormat:@"%li",[[op objectForKey:@"isSign"] integerValue]];
    if ([isSign integerValue] == 2) {
        [_btnQD setTitle:@"已签到" forState:UIControlStateNormal];
        [calendarView setStyle_Today_Signed:calendarView.dayButton];
    }
    
}

- (IBAction)clickQD:(id)sender {
    if ([_btnQD.titleLabel.text isEqualToString:@"已签到"]) {
//        [self showAlert:@"您今天已经签过到了"];
        [LICGToast showWithText:@"您今天已经签过到了"];
        return;
    }
    
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/userSign",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"签到res:%@",responseObject);
        NSString *status = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        if ([status integerValue] == 200) {
            
            //日期点击事件
            __weak typeof(HYCalendarView) *weakDemo = calendarView;
            calendarView.calendarBlock =  ^(NSInteger day, NSInteger month, NSInteger year){
                [weakDemo setStyle_Today_Signed:weakDemo.dayButton];
                
            };
            [self loadData];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

//用户签到日历表展示
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/userSignCalendarWatch",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        NSString *status = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        NSString *str1 = [responseObject objectForKey:@"userHeadPortrait"];
        
        NSString *str2 = [responseObject objectForKey:@"userNickName"];
        NSLog(@"str1=%@,str2=%@",str1,str2);
        if ([status integerValue] == 200) {
            _dic = responseObject;
            array = [responseObject objectForKey:@"content"];
            if ([str1 isKindOfClass:[NSNull class]]) {
                _imgUrl = @"";
            }else{
                _imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],str1];
            }
            
            if ([str2 isKindOfClass:[NSNull class]]) {
                _name = @"匿名";
            }else{
                _name = str2;
            }
            
        }
        
        [self loadUI];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
